//!
/**
 ***********************************************************************************************************
 * <RBD3D9SystemDesc Implementation>
 ***********************************************************************************************************
 * @file RBD3D9SystemDesc.cpp
 ***********************************************************************************************************/

/***********************************************************************************************************
 * Precompiled Header
 ***********************************************************************************************************/
#include "RBD3D9RendererPCH.h"

/***********************************************************************************************************
 * Includes
 ***********************************************************************************************************/
#include "RBD3D9SystemDesc.h"
#include <RBMain/RBContainer.h>

/***********************************************************************************************************
 * Defines
 ***********************************************************************************************************/

/***********************************************************************************************************
 * Classes implementation
 ***********************************************************************************************************/

const D3DFORMAT RBD3D9SystemDesc::ms_aPixelFormats[ms_uiNbPixelFormats] = {
                                                                             D3DFMT_A8R8G8B8,
                                                                             D3DFMT_X8R8G8B8,
                                                                             D3DFMT_R5G6B5,
                                                                             D3DFMT_X1R5G5B5,
                                                                             D3DFMT_A1R5G5B5, 
                                                                             D3DFMT_A2R10G10B10,
                                                                         };

/***********************************************************************************************************
 * Constructor
 ***********************************************************************************************************/
RBD3D9DeviceDesc::RBD3D9DeviceDesc(IDirect3D9* pD3DObject, uint32 uiAdapter, D3DDEVTYPE d3dDevType, D3DFORMAT)
: m_d3dDevType(d3dDevType)
{
    pD3DObject->GetDeviceCaps(uiAdapter, m_d3dDevType, &m_d3dCaps);
}

/**************************************************************************************************************************************************************
 * Constructor
 **************************************************************************************************************************************************************/
RBD3D9AdapterDesc::RBD3D9AdapterDesc(IDirect3D9* pD3DObject, uint32 uiAdapter)
: m_uiAdapter   (uiAdapter)
, m_pHALDeviceDesc (NULL)
{
    pD3DObject->GetAdapterIdentifier(m_uiAdapter, 0, &m_d3dAdapterIdentifier);
    
    D3DDISPLAYMODE d3dDisplayMode;
    // Iterates through all pixel formats
    for (uint32 uiPixelFormat=0; uiPixelFormat<RBD3D9SystemDesc::ms_uiNbPixelFormats; ++uiPixelFormat)
    {
        D3DFORMAT d3dCurPixelFormat = static_cast<D3DFORMAT>(RBD3D9SystemDesc::ms_aPixelFormats[uiPixelFormat]);

        uint32 uiAdapterModeCount = pD3DObject->GetAdapterModeCount(m_uiAdapter, d3dCurPixelFormat);
        for (uint32 uiAdapterMode=0; uiAdapterMode<uiAdapterModeCount;++uiAdapterMode)
        {
            if (SUCCEEDED(pD3DObject->EnumAdapterModes(m_uiAdapter, d3dCurPixelFormat, uiAdapterMode, &d3dDisplayMode)))
            {
                // Check if display mode for adapter has not already been added
                ModeDesc newMode(d3dDisplayMode.Width, d3dDisplayMode.Height, d3dDisplayMode.RefreshRate, d3dDisplayMode.Format);
                bool bModeExists = false;
                for (std::vector<ModeDesc>::iterator iter = m_aModes.begin(); iter != m_aModes.end() && !bModeExists; ++iter)
                {
                    if (*iter == newMode)
                    {
                        bModeExists = true;
                    }
                }

                if (!bModeExists)
                {
                    m_aModes.push_back(newMode);
                }
            }
        }
    }

    D3DDISPLAYMODE d3dCurrentDisplayMode;
    pD3DObject->GetAdapterDisplayMode(m_uiAdapter, &d3dCurrentDisplayMode);

    m_pHALDeviceDesc = RBNew RBD3D9DeviceDesc(pD3DObject, m_uiAdapter, D3DDEVTYPE_HAL, d3dCurrentDisplayMode.Format);

}

/**************************************************************************************************************************************************************
 * Destructor
 **************************************************************************************************************************************************************/
RBD3D9AdapterDesc::~RBD3D9AdapterDesc()
{
    m_aModes.clear();
    RBDelete m_pHALDeviceDesc;
}
/**
 ***********************************************************************************************************
 * Constructor
 ***********************************************************************************************************/
RBD3D9SystemDesc::RBD3D9SystemDesc(IDirect3D9* pD3DObject)
{
    uint32 uiAdapterCount = pD3DObject->GetAdapterCount();
    for (uint32 uiAdapter=0; uiAdapter<uiAdapterCount; ++uiAdapter)
    {
        m_aAdaptersDesc.push_back(RBNew RBD3D9AdapterDesc(pD3DObject, uiAdapter));
    }
}

/**
 ***********************************************************************************************************
 * Destructor
 ***********************************************************************************************************/
RBD3D9SystemDesc::~RBD3D9SystemDesc()
{
    for (uint32 uiAdapter=0; uiAdapter<m_aAdaptersDesc.size(); ++uiAdapter)
    {
        RBDelete m_aAdaptersDesc[uiAdapter];
    }
    m_aAdaptersDesc.clear();
}

/***********************************************************************************************************/

